layui.define(['dataset'], function(exports){
    let dataset = layui.dataset;
    function storage(name){
        if (!(this instanceof storage)) {
            return new storage(name,arguments[1]);
        }
        this.name = Array.isArray(name)?name.join('-'):name;
        let values = arguments[1];
        if(!values){
            let d = localStorage[this.name];
            values = d ? JSON.parse(d) : {};
        }

        this.dataset = dataset(values);

        this.get = function(){
            return this.dataset.get.apply(this.dataset,arguments);
        }

        this.set = function(){
            let ret = this.dataset.set.apply(this.dataset,arguments);
            if(ret !== false){
                this.save();
            }
            return ret;
        }

        this.save = function(){
            localStorage[this.name] = JSON.stringify(this.dataset.get());
        }
    }

    exports('storage', storage)
});